package cn.com.bluemoon.daps.system.entity;

import cn.com.bluemoon.daps.common.domain.BaseModel;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.toolkit.AesEncryptUtils;
import cn.com.bluemoon.daps.common.validation.datasource.IllegalNumber;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Data
@EqualsAndHashCode(callSuper = true)
@Accessors
@ApiModel(value = "DapSystemDatasourceEnvironment", description = "数据源环境表")
public class DapSystemDatasourceEnvironment extends BaseModel<DapSystemDatasourceEnvironment> {

    @ApiModelProperty(value = "关联系统数据源表id")
    private String datasourceId;

    @ApiModelProperty(value = "数据源环境，1-4表示开发、测试、预生产、生产")
    @NotNull(message = "数据源环境不允许为空")
    @IllegalNumber( name= "environment")
    private Integer environment;

    @ApiModelProperty(value = "数据源地址")
    @NotNull(message = "数据源IP地址不允许为空")
//    @Size(max = 9999, message = "数据源IP地址不超过{max}")
    // @Pattern(regexp = "((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)$",message = "ip地址格式不正确")
    private String host;

    @ApiModelProperty(value = "数据(源)库密码, md5加密")
    @Size(max = 200, message = "密码设置不超过32")
    //@NotNull(message = "数据源密码不允许为空")
    private String password;

    @ApiModelProperty(value = "数据源端口")
    @NotNull(message = "数据源端口不允许为空")
    @Max(value = 65535, message = "端口号只能为数字，且不能超过65535")
    @Min(value = 0, message = "端口号只能为数字，且为正整数")
    private Integer port;

    @ApiModelProperty(value = "数据库名")
    @Size(max = 64, message = "数据库名称不能超过64")
    @NotNull(message = "数据库名不允许为空")
    private String databaseName;

    @ApiModelProperty(value = "数据库模式名")
    // @Pattern(regexp = "^[0-9a-zA-Z_]{1,}$", message = "数据库模式名格式不正确，由大小写字母、数字、下划线组成")
    @NotNull(message = "数据库模式名不允许为空")
    @Size(max = 64, message = "数据库对应的模式名不能超过64")
    private String databaseSchema;

    @ApiModelProperty(value = "用户名")
    @NotNull(message = "用户名不允许为空")
    @Size(max = 32, message = "用户名不能超过32")
    private String username;

    @ApiModelProperty(value = "数据库版本")
    @Size(max = 64, message = "数据库版本不能超过{max}")
    private String databaseVersion;

    @ApiModelProperty(value = "ssl连接，false 表示禁用， true 表示激活")
    // @NotNull(message = "ssl连接不允许为空")
    private Boolean sslConnect;

    @ApiModelProperty("SSL证书")
    @Size(max = 128, message = "SSL证书不能超过128字符")
    private String sslCertificate;

    @ApiModelProperty("是否为独立元库，false 表示否，true表示是")
    private Boolean isInfoResource;

    @ApiModelProperty("数据库类型，用于独立元库，默认为1 mysql 2 pgsql 3 hive")
    @EnumValue
    private DatabaseType dbType;

    @ApiModelProperty("数据库引擎，用于hive数据库， 1：原生mr、2：impala、3：spark thrift server")
    private Integer executionEngine;

    @ApiModelProperty("数据库角色，1表示主库，2表示从库")
    private Integer datasourceRole;

    @ApiModelProperty("服务模块，1-4表示模型同步、元数据采集、数据服务、数据安全查询，逗号隔开")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String serverModule;

    public String decryptPassword() {
        try {
            return AesEncryptUtils.decrypt(getPassword());
        } catch (Exception e) {
//            log.error("解密失败，取原值！", e);
            // nothing
            return getPassword();
        }
    }
}
